package com.shine.rabbitmq.converter;

import com.alibaba.fastjson2.JSON;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConverter;

public class FastJsonMessageConverter implements MessageConverter {

    @Override
    public Message toMessage(Object object, MessageProperties messageProperties) {
        String json = JSON.toJSONString(object);
        return new Message(json.getBytes(), messageProperties);
    }

    @Override
    public Object fromMessage(Message message) {
        String json = new String(message.getBody());
        return JSON.parseObject(json, Object.class); // 可以根据需要更改为指定的类类型
    }

}
